package SHIYAN;

import java.util.List;
import java.util.Scanner;

public class UserDaoTest {

	private String name;
	private int    age;
	private String address;
	private double salary;
	private int    id;
	public static final String[] titles = { "ID", "NAME", "SEX", "EMAIL", };

	public static void main(String[] args) {
		menu();
	}

	public static void menu() {
		
		while(true) {
		System.out.println("########please enter your option########");
		System.out.println("\t 1.add user recorder");
		System.out.println("\t 2.display user recorder");
		System.out.println("\t 3.delete  user recorder");
		System.out.println("\t 4.exit system");
		Scanner scanner = new Scanner(System.in);
		String select = scanner.next();


		if ("1".equalsIgnoreCase(select)) {

			testInsert();
			
		} else if ("2".equalsIgnoreCase(select)) {

			testQuery();
			
		} else if ("3".equalsIgnoreCase(select)) {
			testdelete();
		} else if ("4".equalsIgnoreCase(select)) {
			System.out.println("exit!");
			break;
		} else {
			System.out.println("enter error!!!");
		}

		}
		
	}
	
	
	private static void testdelete() {
		// TODO Auto-generated method stub
		System.out.println("please enter delete user information");
		Scanner input = new Scanner(System.in);
		System.out.println("user name:");
		String username = input.next();
		System.out.println("user sex:");
		String usersex = input.next();
		
		
		User user = new User(username, usersex, "");
		
		int insertCount = UserDao.delete(user);
		System.out.println(insertCount > 0 ? "delete success" : "delete failure");
	}

	public static void testInsert() {
		Scanner input = new Scanner(System.in);
		System.out.println("user name:");
		String username = input.next();
		
		System.out.println("user sex:");
		String usernsex = input.next();
		System.out.println("user email:");
		String useremail = input.next();
		
		User user = new User(username, usernsex, useremail);
		
		int insertCount = UserDao.save(user);
		
		System.out.println(insertCount > 0 ? "insert success" : "insert failure");

	}

	private static void testQuery() {
		// TODO Auto-generated method stub

		List<User> list = UserDao.getAllRecords();

		// print titles of company table
		String format = "%-10s";
		for (int i = 0; i < titles.length; i++) {
			System.out.printf(format, titles[i]);
		}
		System.out.println();

		for (int i = 0; i < list.size(); i++) {
			User rs = (User) list.get(i);

			System.out.printf(format, rs.getId());
			System.out.printf(format, rs.getName());
			System.out.printf(format, rs.getSex());
			System.out.printf(format, rs.getEmail());

			System.out.println();

		}
	}

}
